package com.zheng.wechat.mp.admin.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.JsonUtil;
import com.zheng.common.util.PropertiesUtil;
import com.zheng.wechat.mp.common.constant.WechatMpConstant;
import com.zheng.wechat.mp.common.constant.WechatMpResult;
import com.zheng.wechat.mp.common.constant.WechatMpResultConstant;
import com.zheng.wechat.mp.dao.model.*;
import com.zheng.wechat.mp.rpc.api.WechatMpArticleImageService;
import com.zheng.wechat.mp.rpc.api.WechatMpArticleService;
import com.zheng.wechat.mp.rpc.api.WechatMpNoticeService;
import com.zheng.wechat.mp.rpc.api.WechatMpWebJwidService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2018/11/8.
 */
@Controller
@RequestMapping("/manage/notice")
@Api(value = "公告控制器", description = "公告控制器")
public class NoticeController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private WechatMpNoticeService wechatMpNoticeService;

    /**
     * 文章首页
     * @return
     */
    @ApiOperation(value = "公告首页")
    @RequiresPermissions("wechatmp:notice:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/notice/index.jsp";
    }

    @ApiOperation(value = "公告列表")
    @RequiresPermissions("wechatmp:notice:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, defaultValue = "", value = "title") String title,
            @RequestParam(required = false, defaultValue = "", value = "status") Integer status,
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        WechatMpNoticeExample wechatMpNoticeExample = new WechatMpNoticeExample();
        WechatMpNoticeExample.Criteria criteria = wechatMpNoticeExample.createCriteria();
        criteria.andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            wechatMpNoticeExample.setOrderByClause(sort + " " + order);
        }else{
            wechatMpNoticeExample.setOrderByClause("orders asc");
        }
        if (StringUtils.isNotBlank(title)) {
            criteria.andTitleLike("%" + title + "%");
        }else if (StringUtils.isNotBlank(search)) {
            criteria.andTitleLike("%" + search + "%");
        }
        if(status!=null && status != -1){
            criteria.andStatusEqualTo(status.byteValue());
        }
        List<WechatMpNotice> rows = wechatMpNoticeService.selectByExampleForOffsetPage(wechatMpNoticeExample, offset, limit);
        long total = wechatMpNoticeService.countByExample(wechatMpNoticeExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "创建公告")
    @RequiresPermissions("wechatmp:notice:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(ModelMap modelMap) {
        return "/manage/notice/create.jsp";
    }

    @ApiOperation(value = "创建公告")
    @RequiresPermissions("wechatmp:notice:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(WechatMpNotice wechatMpNotice) {
        String loginUserName = getLoginUserName();
        wechatMpNotice.setFlag(Constant.CONST_FLAG_NORMAL);
        wechatMpNotice.setCuser(loginUserName);
        wechatMpNotice.setCtime(System.currentTimeMillis());
        int count = wechatMpNoticeService.insertSelective(wechatMpNotice);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改公告")
    @RequiresPermissions("wechatmp:notice:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        WechatMpNotice wechatMpNotice = wechatMpNoticeService.selectByPrimaryKey(id);
        modelMap.put("wechatMpNotice", wechatMpNotice);
        return "/manage/notice/update.jsp";
    }

    @ApiOperation(value = "修改公告")
    @RequiresPermissions("wechatmp:notice:update")
    @ResponseBody
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    public Object update(@PathVariable("id") int id,WechatMpNotice wechatMpNotice) {
        wechatMpNotice.setId(id);
        int count = wechatMpNoticeService.updateByPrimaryKeySelective(wechatMpNotice);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除公告")
    @RequiresPermissions("wechatmp:notice:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = wechatMpNoticeService.deleteByPrimaryKeys(ids);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }

}
